/****************************************************************************//**
  \file timerInterval.h

  \brief Implementation of timer interval interface.

  \author
    Atmel Corporation: http://www.atmel.com \n
    Support email: avr@atmel.com

  Copyright (c) 2008-2013, Atmel Corporation. All rights reserved.
  Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
  History:
    28.02.11 A. Khromykh - Created
*******************************************************************************/

#ifndef _TIMERINTERVAL_H
#define _TIMERINTERVAL_H

/******************************************************************************
                   Define(s) section
******************************************************************************/
//#define COUNTER_200MS   0xB4

#include <avr/interrupt.h>
/******************************************************************************
                     Inline static functions section
******************************************************************************/
/**************************************************************************//**
\brief Timer initialization (interval 200ms).
******************************************************************************/
static inline void timerIntervalInit(void)
{
	// we must use timer1 because we use 16Mhz clock
	TCCR1B = (1 << WGM12);	// CTC mode 4
	TCCR1A = 0;
	OCR1A = 62500;			// f = 16000000 / 1024 / 62500 = 0.25Hz == wait 4s
	TIFR1 = 0;
}

/**************************************************************************//**
\brief Clear timer settings.
******************************************************************************/
static inline void timerIntervalUnInit(void)
{
	TCCR1A = 0;
	OCR1A = 0;
	TCNT1 = 0;
	TIFR1 = 0;
}

/**************************************************************************//**
\brief Start timer interval.
******************************************************************************/
static inline void timerIntervalStart(void)
{
	TCNT1 = 0;
	TIFR1 = (1 << OCF1A);
	TCCR1B |= (1 << CS12) | (1 << CS10);
}

/**************************************************************************//**
\brief Stop timer.
******************************************************************************/
static inline void timerIntervalStop(void)
{
	TCCR1B = 0;
}

/**************************************************************************//**
\brief Return expired state.

\return
  true - timer expired; \n
  false - otherwise.
******************************************************************************/
static inline bool timerReadIntervalState(void)
{
	return TIFR1 & (1 << OCF1A);
}

#endif /* _TIMERINTERVAL_H */
